<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据驱动</title>
</head>

<body>
    <div>商品数量 <input id="goodsnum" /></div>
    <div>商品价格 <input id="goodsprice" /></div>
    <div>小计 <span id="total"></span></div>
</body>
<script>
    let goodsnumInput = document.getElementById("goodsnum");
    let goodspriceInput = document.getElementById("goodsprice");
    let  totalSpan = document.getElementById("total");
    let goodsInfo = {

    }
    //数据驱动  自定义对象的属性
    let _goodsprice;
    let _goodsnum;
    Object.defineProperty(goodsInfo,"goodsnum",{
        get(){//获取属性的值
            return  _goodsnum;
        },
        set(num){ //设置属性的值
               _goodsnum=num;//全局变量
               complier();//渲染页面
        }
    })

    Object.defineProperty(goodsInfo,"goodsprice",{
        get(){
            return  _goodsprice;
        },
        set(price){
            _goodsprice = price;
            complier();//渲染页面
        }
    })

    function  complier(){//解析页面
        totalSpan.innerHTML = goodsInfo.goodsnum * goodsInfo.goodsprice;

    }

    goodsnumInput.oninput = function(){//文本框架有内容改变会触发
        goodsInfo.goodsnum =this.value;  //操作的是 数据
   
    }
    goodspriceInput.oninput = function(){//文本框架有内容改变会触发
        goodsInfo.goodsprice = this.value;//操作的是  数据
    }



</script>

</html>